﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace VersionComparer.DataModel
{
    /// <summary>
    /// This class represents an exclusion rule. An exclusion rule is applied to a given node so that differences in that node are ignored and count as warnings instead of errors
    /// </summary>
    public class ExclusionRule
    {
        #region statics
        public static string ItemKey(ComparableItem item)
        {
            string key = "";
            if (item.Parent != null)
                key = string.Format("{0}{1}/", ItemKey(item.Parent), item.Text);
            else
                key = string.Format("/");
            return key;
        }
        #endregion statics

        #region constructors
        public ExclusionRule()
        {
        }
        public ExclusionRule(ComparableItem item)
        {
            Scope = RuleScope.SingleFile;
            RootText = item.Root.Text;
            ItemPath = ItemKey(item);
        }
        #endregion constructors

        #region properties
        public string RootText { get; set; }
        public string ItemPath { get; set; }
        public RuleScope Scope { get; set; }
        #endregion properties
      
        #region methods

        public bool WasCreatedOverNode(ComparableItem node)
        {
            return AppliesToRootItem(node.Root) && ItemKey(node) == ItemPath;
        }
        public bool IsSubRuleOfItem(ComparableItem node)
        {
            return AppliesToRootItem(node.Root) && ItemPath.StartsWith(ItemKey(node));
        }
        public bool AppliesToRootItem(ComparableItem node)
        {
            return RootText == node.Text;
        }
        public bool AppliesToNode(ComparableItem node)
        {

            return AppliesToRootItem(node.Root) && ItemKey(node).StartsWith(ItemPath);
        }
        #endregion methods

    }
}
